package com.yanqu.road.dao.impl.activity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.ActivityUserDigFloor;
import com.yanqu.road.entity.activity.huashigang.HuaShiGangFloorCellData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * @Author wpj
 * @Date 2020/10/11
 */
public class ActivityFloorDaoImpl extends TempDataObjectDao<ActivityUserDigFloor> {

    public static String getActivityUserDigFloorSql = "select * from t_u_activity_dig_floor where userId=?";

    public Map<Integer, ActivityUserDigFloor> getActivityUserDigFloor(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, ActivityUserDigFloor> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getActivityUserDigFloorSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                ActivityUserDigFloor ActivityUserDigFloor = parse(rs);
                map.put(ActivityUserDigFloor.getActivityId(), ActivityUserDigFloor);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateActivityUserDigFloorSql = "update t_u_activity_dig_floor set floor=?,cellDataList=?,gotBigReward=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(ActivityUserDigFloor ActivityUserDigFloor) {
        ActivityUserDigFloor.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, ActivityUserDigFloor.getFloor()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(ActivityUserDigFloor.getCellDataList())));
        para.put(i++, new DbParameter(Types.INTEGER, ActivityUserDigFloor.getGotBigReward()));
        para.put(i++, new DbParameter(Types.INTEGER, ActivityUserDigFloor.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, ActivityUserDigFloor.getUserId()));
        if (getDb().executeNoneQuery(updateActivityUserDigFloorSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertActivityUserDigFloorSql = "insert into t_u_activity_dig_floor(activityId,userId,floor,cellDataList,gotBigReward) values (?,?,?,?,?)";

    @Override
    protected boolean addData(ActivityUserDigFloor ActivityUserDigFloor) {
        ActivityUserDigFloor.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, ActivityUserDigFloor.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, ActivityUserDigFloor.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, ActivityUserDigFloor.getFloor()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(ActivityUserDigFloor.getCellDataList())));
        para.put(i++, new DbParameter(Types.INTEGER, ActivityUserDigFloor.getGotBigReward()));
        if (getDb().executeNoneQuery(insertActivityUserDigFloorSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected ActivityUserDigFloor parseData(ResultSet rs) throws SQLException {
        ActivityUserDigFloor ActivityUserDigFloor = new ActivityUserDigFloor();
        ActivityUserDigFloor.setActivityId(rs.getInt("activityId"));
        ActivityUserDigFloor.setUserId(rs.getLong("userId"));
        ActivityUserDigFloor.setFloor(rs.getInt("floor"));
        ActivityUserDigFloor.setCellDataList(JSON.parseObject(rs.getString("cellDataList"), new TypeReference<List<HuaShiGangFloorCellData>>() {
        }));
        ActivityUserDigFloor.setGotBigReward(rs.getInt("gotBigReward"));
        return ActivityUserDigFloor;
    }

}
